﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using SLS.ExClassLib.Database;
using SLS.ExClassLib.DataType;
using SLS.ExClassLib.Message;
namespace SLS.ExClassLib.Data
{
    public partial class ExDataRow:Object
    { 
        public string TBName
        {
            get { return Item.Table.TableName; }
        }
        public ArrayList DataList
        {
            get { return DataAccessService.Instance.DataList; }
        }

        public ExDataRow MergeDataList(string INSTANCEID, DataTable tbThis = null, string TBType = "Set2Create")
        {
            ExDataRow dwReturn = null;
            try
            {
                var query = from ExDataRow datalist in  DataList
                            where datalist.INSTANCEID == INSTANCEID
                            select datalist;
                foreach (ExDataRow exdw in query)
                {
                    dwReturn = exdw;
                    if (tbThis == null)
                        continue;
                    if (tbThis.Rows.Count == 0)
                        continue;
                    switch (TBType)
                    {
                        case "Set2Create":
                            exdw.AddSet2Create(tbThis.TableName, tbThis);
                            break;
                        case "Set4Reference":
                            exdw.Set4Reference.Merge(tbThis);
                            break;
                        case "Set5KeyDef":
                            exdw.Set5KeyDef.Merge(tbThis);
                            break;
                        case "Set5Property":
                            exdw.Set5Property.Merge(tbThis);
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return dwReturn;
        }
        public bool ValidateGoodsMasterKeyRelation = true;
        public string GDKEY
        {
            get
            {
                return Item["GDKEY"].ToString();
            }
            set
            {
                Item["GDKEY"] = value;
                dwGoodsMasterKeyRelation["GDKEY"] = value;
            }
        }
        public DataRow dwGoodsMasterKeyRelation
        {
            get
            { 
                if (!Set2Create .Tables.Contains("GoodsMasterKeyRelation"))
                    return null;
                if (Set2Create.Tables["GoodsMasterKeyRelation"].Rows.Count == 0)
                {
                    return null; 
                }
                DataRow row = null;
                row = Set2Create.Tables["GoodsMasterKeyRelation"].Rows[0];
                return row;
            }
            set
            { }
        }
        public string DataSet2String(DataSet dsSource)
        {
            string DataSetStr = "";
            if (dsSource == null)
            {
                return "DataSet is null";
            }
            foreach (DataTable dtTemp in dsSource.Tables)
            {
                DataSetStr = "Tables: " + dtTemp + ";";
                int i = 0;
                foreach (DataRow dwTemp in dtTemp.Rows)
                {
                    i++;
                    DataSetStr += "Line " + i.ToString() + ":";
                    DataSetStr += ";";
                    foreach (DataColumn dwColumn in dtTemp.Columns)
                    {
                        DataSetStr += dwColumn.ColumnName + ":" + dwTemp[dwColumn.ColumnName].ToString().Trim();
                        DataSetStr += ";";
                    }
                    DataSetStr += ";";
                }
            }
            return DataSetStr;
        }
        public ExDataRow()
        { 
            
        }
        public ExDataRow(DataRow dwThis, bool CheckDataQuality = false)
        {   
            IsCheckDataQuality =CheckDataQuality;
            if (dwThis != null)
            {
                Table = dwThis.Table.Clone();
                m_DataRow = Table.NewRow();
                m_DataRow.ItemArray = dwThis.ItemArray;
                Table.Rows.Add(m_DataRow);
            }
        }
        public ArrayList Table2CreateList = new ArrayList();
        public DataSet Set4Reference = new DataSet();
        public DataTable Table;
        public DataSet Set2Create = new DataSet();
        public DataSet Set5KeyDef = new DataSet();
        public DataSet Set5Property = new DataSet();
        public string SUBSTATUS
        {
            get
            {
                return m_DataRow["SUBSTATUS"].ToString();
            }
            set
            {
                m_DataRow["SUBSTATUS"] = value;
            }
        }
        public string STATUS
        {
            get
            {
                return m_DataRow["STATUS"].ToString();
            }
            set
            {
                m_DataRow["STATUS"] = value;
            }
        }
        public string INSTANCEID
        {
            get
            {
                return m_DataRow["INSTANCEID"].ToString();
            } 
        }
        public MessageEx CallerMessage
        {
            get;
            set;
        }
        public bool PropertyTableCreated = false;
        public bool IsOnRunSelectedRecord = false;
        public bool IsCheckDataQuality = false;
        private DataRow m_DataRow;
        public DataRow Item
        {
            get
            {
                return m_DataRow;
            }
            set
            {
                m_DataRow = value;
            }
        }
        public override string ToString()
        {
            return base.ToString();
        }
        public void Clear()
        {
            try
            {
                Set2Create.Clear();
                Set4Reference.Clear();
                Set5Property.Clear();
                Set5KeyDef.Clear();
            }
            catch (Exception ex)
            {
                throw;
            }
        } 
        public bool IsTBCreated(string TBName)
        {
            bool blCreated = false;
            try
            {
                Table2Create t2c;
                var query = from Table2Create t2cl in Table2CreateList
                            where t2cl.TBName == TBName
                            select t2cl;
                foreach (Table2Create t2cl in query)
                {
                    blCreated = t2cl.blCreated;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return blCreated;
        }
       
    }
    public partial class ExDataRow : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    } 
}